package com.market.cash.ui.activity

import android.graphics.Color
import android.view.View
import com.market.cash.R
import com.market.cash.databinding.CashStepsBinding
import com.market.cash.pro.ProActivity
import com.market.cash.ui.fragment.StepAccountFragment
import com.market.cash.ui.fragment.StepCommonFragment
import com.market.cash.ui.fragment.StepContactFragment
import com.market.cash.ui.fragment.StepOcrFragment
import com.market.cash.ui.fragment.StepSelfieFragment
import com.market.cash.ui.presenter.CashRootStepsPresenter
import com.market.cash.ui.view.ICashRootStepsView
import com.market.cash.utils.CashRootPickContactUtils
import com.market.cash.utils.event.CashRootEventData
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCustomConfigsHelper
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/21 18:51
 * @Annotation：进件流程
 */
open class StepsActivity: ProActivity(), ICashRootStepsView {
    lateinit var binding: CashStepsBinding

    var presenter: CashRootStepsPresenter? = null
    //选择联系人
    var pickContact: CashRootPickContactUtils? = null
    override fun getCashRootRootView(): View? {
        binding = CashStepsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#FFFFFF"))
        setCashRootTitleBarVisibility(View.VISIBLE)
        setCashRootRightBtnRes(R.mipmap.cash_kefu_black)

        pickContact = CashRootPickContactUtils(this)
    }

    override fun initCashRootListener() {
        rootViewBinding.imgKefu.setOnClickListener {
            val eventTitle = presenter!!.getCashRootEventStepTitle()
            KefuActivity.start(this,eventTitle)
            addCashRootEventPoint(CashRootEventType.CLICK.CASHROOT_CLICK_CUSTOMER,eventTitle)
        }
    }

    override fun initCashRootData() {
        presenter = CashRootStepsPresenter(this)
        presenter!!.loadCashRootStepPageAction()

        //牌照信息
        if (!CashRootCustomConfigsHelper.isCashRootShowLicense()){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(initCashRootLicenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    /**
     * 所有步骤已完成
     */
    override fun showCashRootStepsComplete() {
        //关闭当前页面
        finish()
    }

    /**
     * 设置当前进度
     */
    override fun showCashRootStepsProgress(showTitle: String,max: Int, progress: Int) {
        setCashRootTextTitle(showTitle)
        binding.progressBar.max = max
        binding.progressBar.progress = progress
    }

    /**
     * 联系人
     */
    override fun showCashRootStepContactView(stepData: JSONObject) {
        replaceContainerFragement(R.id.stepPageLayout, StepContactFragment(this,stepData))
    }

    /**
     * 证件照
     */
    override fun showCashRootStepOcrView(stepData: JSONObject) {
        replaceContainerFragement(R.id.stepPageLayout,StepOcrFragment(stepData))
    }

    /**
     * 人脸照
     */
    override fun showCashRootStepSelfieView(stepData: JSONObject) {
        replaceContainerFragement(R.id.stepPageLayout,StepSelfieFragment(stepData))
    }

    /**
     * 绑卡
     */
    override fun showCashRootStepAccountView(stepData: JSONObject) {
        replaceContainerFragement(R.id.stepPageLayout, StepAccountFragment(stepData))
    }

    /**
     * 通用样式
     */
    override fun showCashRootStepCommonView(stepData: JSONObject) {
        replaceContainerFragement(R.id.stepPageLayout, StepCommonFragment(stepData))
    }

    /**
     * 提交
     */
    open fun onCashRootSubmitAction(params: JSONObject){
        presenter!!.submitCashRootProcessAction(params)
    }

    /**
     * 流程提交成功
     * 需要发送通知刷新总流程
     */
    override fun showCashRootSubmitSuccess() {
        //通知刷新总流程
        publishEventData(CashRootEventData(CashRootEventData.TYPE.CASHROOT_PROCESS_REFRESH))
    }

    override fun onCashRootEvent(event: CashRootEventData) {
        super.onCashRootEvent(event)
        when(event.eventType){
            CashRootEventData.TYPE.CASHROOT_PROCESS_REFRESHED -> {
                presenter!!.loadCashRootStepPageAction()
            }
            else -> {

            }
        }
    }

    override fun onStop() {
        super.onStop()
        binding.root.clearFocus()
    }
}